package com.mornd.gulimall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mornd.gulimall.common.core.result.AjaxResult;
import com.mornd.gulimall.common.core.validation.UpdateGroup;
import com.mornd.gulimall.product.entity.AttrAttrgroupRelation;
import com.mornd.gulimall.product.entity.AttrGroup;
import com.mornd.gulimall.product.service.IAttrAttrgroupRelationService;
import com.mornd.gulimall.product.service.IAttrGroupService;
import com.mornd.gulimall.product.service.ICategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;

import java.util.Arrays;
import java.util.List;

import static com.mornd.gulimall.common.core.result.AjaxResult.error;
import static com.mornd.gulimall.common.core.result.AjaxResult.success;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */

@Validated
@RestController
@RequestMapping("/product/attrGroup")
public class AttrGroupController {
    @Resource
    private IAttrGroupService attrGroupService;
    @Resource
    private ICategoryService categoryService;
    @Resource
    private IAttrAttrgroupRelationService attrgroupRelationService;

    @GetMapping
    public AjaxResult list(AttrGroup attrGroup) {
        IPage<AttrGroup> page = attrGroupService.list(attrGroup);
        return success(page);
    }

    @GetMapping("/selectList/{catId}")
    public AjaxResult selectList(@PathVariable Long catId) {
        List<AttrGroup> list = attrGroupService.list(Wrappers.<AttrGroup>lambdaQuery().eq(AttrGroup::getCatelogId, catId));
        return success(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        AttrGroup entity = attrGroupService.getById(id);
        // 获取 category 的完整路径
        entity.setCatelogFrom(categoryService.from(entity.getCatelogId()));
        return success(entity);
    }

    @PutMapping
    public AjaxResult save(@RequestBody @Validated AttrGroup attrGroup) {
        if (!categoryService.isLeaf(attrGroup.getCatelogId())) {
            return error("必须选择最后一层分类");
        }
        attrGroup.setAttrGroupId(null);
        attrGroupService.save(attrGroup);
        return success();
    }

    @PostMapping
    public AjaxResult update(@RequestBody @Validated(UpdateGroup.class) AttrGroup attrGroup) {
        if (!categoryService.isLeaf(attrGroup.getCatelogId())) {
            return error("必须选择最后一层分类");
        }
        attrGroupService.updateById(attrGroup);
        return success();
    }

    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable @NotEmpty Long[] ids) {
        for (Long id : ids) {
            long count = attrgroupRelationService.count(Wrappers.<AttrAttrgroupRelation>lambdaQuery().eq(AttrAttrgroupRelation::getAttrGroupId, id));
            if (count > 0) {
                return error("删除失败，所选的分组中有被属性关联的数据");
            }
        }
        attrGroupService.removeByIds(Arrays.asList(ids));
        return success();
    }
}
